<?php
namespace Admin\Controller;
use Admin\Controller\AdminBaseController;
use Common\Model\BonusLogModel;
use Admin\Form\UserForm;
use Common\Model\BonusTypeModel;
use Common\Model\UserMessageModel;
use Common\Model\UploadModel;
use Think\Upload;
class ServiceController extends AdminBaseController{
	public function userMessage()
	{
	    $input=I('get.');
	    if ($input['username']) {
	       $user=UserForm::newSelf()->search($input);
	       $where['user_id']=$user['id'];
	    }
	    $where['sendee_id']='0';
	    $list=UserMessageModel::newSelf()->pagingList($where);
	    $this->assign('list',$list);
		$this->display();
	}
    public function backUserMessage()
    {
        $id=I('id');
        $info=UserMessageModel::newSelf()->findOneById($id);
        $this->assign('info',$info);
        $this->display();
    }
    public function backUserMessageFormSub()
    {
        $input=I('post.');
        $username=I('username');
        $this->notEmptyValidate($username, '请填写留言用户');
        $info=$this->findUserByUserName($username, '用户不存在');

        $data['title']=I('title');
        $data['title']=I('content');
        $this->notEmptyValidate($data['title'], '请填写标题');
        $this->notEmptyValidate($data['title'], '请填写内容');

        $data['sendee_id']=$info['id'];
        $data['type']=13;
        $data['add_time']=time();
        $res=UserMessageModel::newSelf()->add($data);
        $this->resReturn($res, '留言成功', '留言失败');
    }
	public function bonusLog()
	{
	    $input=I('get.');
	    if ($input['username']) {
	       $user=UserForm::newSelf()->search($input);
	       $where['user_id']=$user['id'];
	    }
	    if ($input['add_time']) {
	       $where['add_time']=strtotime($input['add_time']);
	    }
        $bonus_type=BonusTypeModel::newSelf()->findInfo();
        
        $list=BonusLogModel::newSelf()->pagingList($where);

        $this->assign('bonus_type',$bonus_type);
        $this->assign('list',$list);
	    $this->display();
	}
	public function uploadLog()
	{

	    $list=UploadModel::newSelf()->pagingList();
	    $this->assign('list',$list);
	    $this->display();
	}
	public function addUpload()
	{
	    $this->display();
	}
	public function delUpload()
	{
	    $id=I('id');
        $res=UploadModel::newSelf()->where(['id'=>$id])->delete();
        $this->resReturn($res, '删除成功', '删除失败');
	}
	public function addUploadSub()
	{
        $data['title']=I('title');
        $this->notEmptyValidate($data['title'],'请填写标题');
        $res=$this->uploadOneFile('file');
        if ($res['status']) {
            $data['add_time']=time();
            $data['url']=$res['info'];
            UploadModel::newSelf()->add($data);
        }
        $this->resReturn($res['status'], '添加成功', $res['info']);
	}
	/**
	 * 文件上传方法
	 * @param string $name 传入图片的name
	 * @return boolean
	 */
	protected function uploadOneFile($name,$title){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','pdf','xlsx','txt','docx');// 设置附件上传类型
		// 		$upload->mimes      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
		$upload->saveName = 'upload'.rand(10000,99999);
		// 上传单个文件
		$info   =   $upload->uploadOne($_FILES[$name]);
		if(!$info) {// 上传错误提示错误信息
			$data['info']=$upload->getError();
			$data['status']=false;
			return $data;
		}else{// 上传成功 获取上传文件信息
			$data['info']='/Uploads/'.$info['savepath'].$info['savename'];
			$data['status']=true;
			return $data;
		}
	}
}
